package org.jslaughter.component.ai;

import org.nvframe.component.AbstractComponent;
import org.nvframe.component.physics.state.PhysicsPosition;
import org.nvframe.entity.Entity;
import org.nvframe.util.settings.SettingsObj;

public class Behaviour extends AbstractComponent {
	
	public enum BEHAVIOUR_TYPE {
		NONE,
		FRIENDLY,
		HOSTILE
	}
	
	private BEHAVIOUR_TYPE behaviour;
	private String team;

	public Behaviour(String id, Entity owner, SettingsObj settings) {
		super(id, owner);
		
		behaviour = BEHAVIOUR_TYPE.valueOf(settings.getString("behaviour", "NONE"));
		team = settings.getString("team", "NONE");
	}

	public BEHAVIOUR_TYPE getBehaviour() {
		return behaviour;
	}

	public void setBehaviour(BEHAVIOUR_TYPE behaviour) {
		this.behaviour = behaviour;
	}
	
	public String getTeam() {
		return team;
	}
	
	public boolean isAggressiveTo(Entity target) {
		boolean isBullet = false;
		PhysicsPosition positionComp = (PhysicsPosition) owner.getComponent(PhysicsPosition.class);
		Behaviour behaviourComp = (Behaviour) target.getComponent(Behaviour.class);
		
		if(positionComp != null)
			isBullet = positionComp.isBullet();
		
		// no behaviour => no aggression
		if(behaviourComp == null)  {
			if(isBullet/*getBehaviour() == BEHAVIOUR_TYPE.HOSTILE*/)
				return true;
			else
				return false;
		}
		
		// when bullet => can't damage root
		if(isBullet && owner.getRoot() == target)
			return false;
		
		// same team => no aggression
		if(behaviourComp.getTeam().equals(getTeam()))
			return false;
		
		// not hostile => no aggression
		if(getBehaviour() != BEHAVIOUR_TYPE.HOSTILE)
			return false;
		
		return true;
	}

}
